1 /*
2  * Copyright (c) 2013-2014 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 module libdvbv5_d.mpeg_ts;
21 
22 import core.sys.posix.unistd;
23 
24 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
25 
26 extern (C):
27 
28 /**
29  * @file mpeg_ts.h
30  * @ingroup dvb_table
31  * @brief Provides the table parser for the MPEG-PES Elementary Stream
32  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
33  * @author Andre Roth
34  *
35  * @par Relevant specs
36  * The table described herein is defined in ISO 13818-1
37  *
38  * @see
39  * http://en.wikipedia.org/wiki/MPEG_transport_stream
40  *
41  * @par Bug Report
42  * Please submit bug reports and patches to linux-media@vger.kernel.org
43  */
44 
45 /* ssize_t */
46 
47 /**
48  * @def DVB_MPEG_TS
49  *	@brief MPEG Transport Stream magic
50  *	@ingroup dvb_table
51  * @def DVB_MPEG_TS_PACKET_SIZE
52  *	@brief Size of an MPEG packet
53  *	@ingroup dvb_table
54  */
55 enum DVB_MPEG_TS = 0x47;
56 enum DVB_MPEG_TS_PACKET_SIZE = 188;
57 
58 /**
59  * @struct dvb_mpeg_ts_adaption
60  * @brief MPEG TS header adaption field
61  * @ingroup dvb_table
62  *
63  * @param type			DVB_MPEG_ES_SEQ_START
64  * @param length		1 bit	Adaptation Field Length
65  * @param discontinued		1 bit	Discontinuity indicator
66  * @param random_access		1 bit	Random Access indicator
67  * @param priority		1 bit	Elementary stream priority indicator
68  * @param PCR			1 bit	PCR flag
69  * @param OPCR			1 bit	OPCR flag
70  * @param splicing_point	1 bit	Splicing point flag
71  * @param private_data		1 bit	Transport private data flag
72  * @param extension		1 bit	Adaptation field extension flag
73  * @param data			Pointer to data
74  */
75 struct dvb_mpeg_ts_adaption
76 {
77     align (1):
78 
79     ubyte length;
80 
81     struct
82     {
83         import std.bitmanip : bitfields;
84         align (1):
85 
86         mixin(bitfields!(
87             ubyte, "extension", 1,
88             ubyte, "private_data", 1,
89             ubyte, "splicing_point", 1,
90             ubyte, "OPCR", 1,
91             ubyte, "PCR", 1,
92             ubyte, "priority", 1,
93             ubyte, "random_access", 1,
94             ubyte, "discontinued", 1));
95     }
96 
97     ubyte[] data;
98 }
99 
100 /**
101  * @struct dvb_mpeg_ts
102  * @brief MPEG TS header
103  * @ingroup dvb_table
104  *
105  * @param sync_byte		DVB_MPEG_TS
106  * @param tei			1 bit	Transport Error Indicator
107  * @param payload_start		1 bit	Payload Unit Start Indicator
108  * @param priority		1 bit	Transport Priority
109  * @param pid			13 bits	Packet Identifier
110  * @param scrambling		2 bits	Scrambling control
111  * @param adaptation_field	1 bit	Adaptation field exist
112  * @param payload		1 bit	Contains payload
113  * @param continuity_counter	4 bits	Continuity counter
114  * @param adaption		Pointer to optional adaption fiels (struct dvb_mpeg_ts_adaption)
115  */
116 struct dvb_mpeg_ts
117 {
118     align (1):
119 
120     ubyte sync_byte;
121 
122     union
123     {
124         align (1):
125 
126         ushort bitfield;
127 
128         struct
129         {
130             import std.bitmanip : bitfields;
131             align (1):
132 
133             mixin(bitfields!(
134                 ushort, "pid", 13,
135                 ushort, "priority", 1,
136                 ushort, "payload_start", 1,
137                 ushort, "tei", 1));
138         }
139     }
140 
141     struct
142     {
143         import std.bitmanip : bitfields;
144         align (1):
145 
146         mixin(bitfields!(
147             ubyte, "continuity_counter", 4,
148             ubyte, "payload", 1,
149             ubyte, "adaptation_field", 1,
150             ubyte, "scrambling", 2));
151     }
152 
153     dvb_mpeg_ts_adaption[] adaption;
154 }
155 
156 // struct dvb_v5_fe_parms;
157 
158 /**
159  * @brief Initialize a struct dvb_mpeg_ts from buffer
160  * @ingroup dvb_table
161  *
162  * @param parms		struct dvb_v5_fe_parms for log functions
163  * @param buf		Buffer
164  * @param buflen	Length of buffer
165  * @param table		Pointer to allocated struct dvb_mpeg_ts
166  * @param table_length	Pointer to size_t where length will be written to
167  *
168  * @return		Length of data in table
169  *
170  * This function copies the length of struct dvb_mpeg_ts
171  * to table and fixes endianness. The pointer table has to be allocated
172  * on stack or dynamically.
173  */
174 ssize_t dvb_mpeg_ts_init (
175     dvb_v5_fe_parms* parms,
176     const(ubyte)* buf,
177     ssize_t buflen,
178     ubyte* table,
179     ssize_t* table_length);
180 
181 /**
182  * @brief Deallocate memory associated with a struct dvb_mpeg_ts
183  * @ingroup dvb_table
184  *
185  * @param ts	struct dvb_mpeg_ts to be deallocated
186  *
187  * If ts was allocated dynamically, this function
188  * can be used to free the memory.
189  */
190 void dvb_mpeg_ts_free (dvb_mpeg_ts* ts);
191 
192 /**
193  * @brief Print details of struct dvb_mpeg_ts
194  * @ingroup dvb_table
195  *
196  * @param parms		struct dvb_v5_fe_parms for log functions
197  * @param ts    	Pointer to struct dvb_mpeg_ts to print
198  *
199  * This function prints the fields of struct dvb_mpeg_ts
200  */
201 void dvb_mpeg_ts_print (dvb_v5_fe_parms* parms, dvb_mpeg_ts* ts);